package com.my.controller;


import com.alibaba.fastjson.JSONObject;
import com.my.bean.DayMoney;
import com.my.bean.ResultPage;
import com.my.bean.User;
import com.my.bean.VoPage;
import com.my.service.DayMoneyService;
import com.my.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器 财务管理  日报
 * </p>
 *
 * @author 张子艺
 * @since 2018-09-15
 */
@Controller
@RequestMapping("/dayMoney")
public class DayMoneyController {
    @Autowired
    DayMoneyService dayMoneyService;
    //主页面
    @RequestMapping("/main.html")
    public String adminInfo(){
        return "dayMoney";
    }
    //新增弹窗
    @RequestMapping("/add.html")
    public String add(){
        return "/Alert/dayMoneyAdd";
    }    //编辑弹窗
    @RequestMapping("/edit.html")
    public String edit(){
        return "/Alert/dayMoneyEdit";
    }
    //新增弹窗
    @RequestMapping("/list")
    @ResponseBody
    public ResultPage<DayMoney> add(String lin, String data, VoPage voPage, HttpSession session){
        boolean admin=false;
        //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }
        Map map=new HashMap();
        map.put("lin",lin);
        map.put("data",data);
        Long areaid = ((User)session.getAttribute("user")).getAreaId();
        return dayMoneyService.selectMyPage(map,voPage,areaid,admin);
    }
    //新增方法
    @RequestMapping("/insertDay")
    @ResponseBody
    public String  insertDay(String param,HttpSession session){
        DayMoney dayMoney=  JSONObject.parseObject(param,DayMoney.class);
        dayMoney.setCreateTime(new Date().getTime());
        Long areaid =((User)session.getAttribute("user")).getAreaId();
        dayMoney.setAreaid(areaid);
        if(dayMoneyService.insert(dayMoney)){
            return "保存成功";
        }
        return "保存失败";

    }
    //编辑方法
    @RequestMapping("/updateDay")
    @ResponseBody
    public String  updateDay(String param){
        DayMoney dayMoney=  JSONObject.parseObject(param,DayMoney.class);
        dayMoney.setCreateTime(new Date().getTime());
        if(dayMoneyService.updateById(dayMoney)){
            return "修改成功";
        }
        return "修改失败";

    }
    //删除方法
    @RequestMapping("/deleteDay")
    @ResponseBody
    public String  deleteDay(Long id){
        if(dayMoneyService.deleteById(id)){
            return "删除成功";
        }
        return "删除失败";

    }

}
